package com.kaizzi.data.ppt.template;

import com.fasterxml.jackson.databind.JsonNode;
import com.kaizzi.data.common.utils.StringUtil;
import com.kaizzi.data.jdl.service.LogService;
import com.kaizzi.data.ppt.TemplateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.sl.usermodel.TableCell;
import org.apache.poi.sl.usermodel.TableShape;
import org.apache.poi.xslf.usermodel.XSLFShape;
import org.apache.poi.xslf.usermodel.XSLFTextParagraph;
import org.apache.poi.xslf.usermodel.XSLFTextRun;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Kzen
 * @Description description
 * @date 2020/9/27
 */
@Component
@Slf4j
public class Template39 {

    @Resource
    private TemplateUtil templateUtil;
    @Resource
    private LogService logService;

    public void text(List<XSLFTextParagraph> textParagraphs,JsonNode data) {
        if(textParagraphs==null||textParagraphs.size()==0){
            return;
        }
        for (XSLFTextParagraph textParagraph : textParagraphs) {
            if(textParagraph==null){
                continue;
            }
            List<XSLFTextRun> textRuns = textParagraph.getTextRuns();
            if(textRuns==null){
                continue;
            }
            for (XSLFTextRun textRun : textRuns) {
                if (textRun == null || textRun.getXmlObject()  == null){
                    return;
                }
                try {
                    if (textRun.getRawText().equals("33KEY-1")){
                        String title = data.get("title").asText();
                        templateUtil.text(textParagraph,title,false);
                        break;
                    }
                    if (textRun.getRawText().equals("33KEY-3")){
                        String title = data.get("title2").get(0).asText();
                        templateUtil.text(textParagraph,title,false);
                        break;
                    }
                    if (textRun.getRawText().equals("33KEY-5")){
                        String title = data.get("title2").get(1).asText();
                        templateUtil.text(textParagraph,title,false);
                        break;
                    }
                    if (textRun.getRawText().equals("33KEY-7")){
                        String title = data.get("title2").get(2).asText();
                        templateUtil.text(textParagraph,title,false);
                        break;
                    }
                    if (textRun.getRawText().equals("33KEY-9")){
                        String title = data.get("dataPeriod").asText();
                        textRun.setText(title);
                        break;
                    }
                }catch (Exception e){
                    logService.setDataLogStatus("ppt-error");
                    log.error("page:{},text Exception:{}",39,e);
                }
            }
        }
    }

    public void table(XSLFShape shape, JsonNode data) {
        TableShape tableShape = (TableShape)shape;
        int numberOfColumns = tableShape.getNumberOfColumns();
        int numberOfRows = tableShape.getNumberOfRows();
        //遍历获取每一个单元格
        for (int row = 1; row<numberOfRows; row++ ){
            for (int col =1 ;col < numberOfColumns; col++){
                TableCell cell = tableShape.getCell(row, col);
                if (cell.getTextParagraphs().size() > 0){
                    XSLFTextParagraph cellParagraphs = (XSLFTextParagraph)cell.getTextParagraphs().get(0);
                    if (cellParagraphs.getTextRuns().size()> 0){
//                        System.out.println("cell : "+getCellData(data,row,col,numberOfRows));
                        cellParagraphs.getTextRuns().get(0).setText(getCellData(data,row,col,numberOfRows));
                    }
                }
            }
        }
    }

    private String getCellData(JsonNode data, int row, int col, int rowNum) {
        String photoId = StringUtil.join(row);
        String jsonKey;
        String jsonKey1;

        if (col == 1){
            jsonKey1 = "name1";
        }else {
            jsonKey1 = "name2";
        }

        if (rowNum > 3) {
            jsonKey = "sports";
        }else {
            jsonKey = "youngs";
        }

        for (JsonNode node : data.get(jsonKey)) {
            if (node.get("photoId").asText().equals(photoId)){
                return node.get(jsonKey1).asText();
            }
        }
        return "";
    }

    public void simpleShape(int red, XSLFShape shape, JsonNode data) {
        if (red ==0 || red %10 !=0){
            return;
        }
        String jsonKey = "";
        int row =0;
        switch (red){
            case 10:
                jsonKey = "sports";
                row = 2;
                break;
            case 20:
                jsonKey = "sports";
                row = 3;
                break;
            case 30:
                jsonKey = "sports";
                row = 4;
                break;
            case 40:
                jsonKey = "sports";
                row = 5;
                break;
            case 50:
                templateUtil.replaceSimpleShape(1,shape);
                return;
            case 60:
                jsonKey = "youngs";
                row = 2;
                break;
            case 70:
                jsonKey = "youngs";
                row = 1;
                break;
            case 80:
                jsonKey = "sports";
                row = 1;
                break;
            default:
                break;
        }
        int trend = getSimpleData(data,jsonKey,row);
        templateUtil.replaceSimpleShape(trend,shape);
    }

    private static int getSimpleData(JsonNode data, String jsonKey, int row){
        int i = data.get(jsonKey).get(row-1).get("trend").asInt();
        return i == 0 ? 1 : i;
    }
}
